package cn.upfinder.datalibrary.Utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;

import cn.upfinder.datalibrary.Bean.VideoEntity;
import okhttp3.ResponseBody;
import rx.functions.Func1;

/**
 * Created by upfinder on 2016/11/27.
 */

public class ParseHtmlUtil {

    private static final String TO_CHARSET_NAME = "utf-8";

    private static Func1<ResponseBody, String> stringFunc1;

    private static Func1<String, ArrayList<VideoEntity>> youKuVideoListFunc;

    private static Func1<String, ArrayList<VideoEntity>> souKuBeautyDanceFunc;

    private static Func1<String, ArrayList<String>> youKuVideoParts;

    public static Func1<ResponseBody, String> getStringFunc1() {
        if (stringFunc1 == null) {
            stringFunc1 = new Func1<ResponseBody, String>() {
                @Override
                public String call(ResponseBody responseBody) {
                    try {
                        String str = new String(responseBody.bytes(), TO_CHARSET_NAME);
                        return str;
                    } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                    }
                }
            };
        }
        return stringFunc1;
    }

    public static Func1<String, ArrayList<VideoEntity>> getYouKuVideoListFunc() {
        if (youKuVideoListFunc == null) {
            youKuVideoListFunc = new Func1<String, ArrayList<VideoEntity>>() {
                @Override
                public ArrayList<VideoEntity> call(String s) {
                    Document document = Jsoup.parse(s);
                    Elements elements = document.select("div.box-series").select("ul.panel");
                    Elements movieItems = elements.select("div.p-thumb");
                    ArrayList<VideoEntity> videoEntityArrayList = new ArrayList<VideoEntity>();
                    for (Element item : movieItems) {
                        VideoEntity videoEntity = new VideoEntity();
                        Element element = item.select("a[href]").first();
                        videoEntity.setUrl(element.attr("href"));
                        videoEntity.setImgUrl(item.select("img").first().attr("src"));
                        videoEntity.setName(item.select("img").first().attr("alt"));
                        videoEntityArrayList.add(videoEntity);
                    }
                    return videoEntityArrayList;
                }
            };
        }
        return youKuVideoListFunc;
    }

    public static Func1<String, ArrayList<VideoEntity>> getSouKuBeautyDanceFunc() {

        if (souKuBeautyDanceFunc == null) {
            souKuBeautyDanceFunc = new Func1<String, ArrayList<VideoEntity>>() {
                @Override
                public ArrayList<VideoEntity> call(String s) {
                    ArrayList<VideoEntity> videoEntityArrayList = new ArrayList<>();
                    Document document = Jsoup.parse(s);


                    Elements videoItemElements = document.select("div.v");
                    for (Element item : videoItemElements) {
                        try {
                            VideoEntity videoEntity = new VideoEntity();
                            // 1: 解析封面图 和视频名称
                            Element thumbElement = item.select("div.v-thumb").select("img").first();
                            String videoName = thumbElement.attr("alt");
                            videoEntity.setName(videoName);
                            String imgUrl = thumbElement.attr("src");
                            videoEntity.setImgUrl(imgUrl);

                            // 2:解析播放地址
                            Element linkElement = item.select("div.v-link").select("a").first();
                            String url = linkElement.attr("href");
                            videoEntity.setUrl(url);

                            // 3:解析播放量和上传时间 上传用户
                            Element metaEntryElement = item.select("div.v-meta-entry").first();
                            String upLoader = metaEntryElement.select("span.username").select("a").text();
                            videoEntity.setUpLoader(upLoader);
                            String upLoadTime = metaEntryElement.select("span.r").text();
                            videoEntity.setUpLoadTime(upLoadTime);
                            String viewNum = metaEntryElement.select("span.pub").text();
                            videoEntity.setViewNum(viewNum);
                            videoEntityArrayList.add(videoEntity);
                        } catch (Exception e) {

                        }

                    }

                    return videoEntityArrayList;
                }
            };
        }
        return souKuBeautyDanceFunc;
    }


    public static ArrayList<String> getYouKuVideoParts(String htmlStr) {
        ArrayList<String> urls = new ArrayList<>();
        Document document = null;
        document = Jsoup.parse(htmlStr);
        Elements parts = document.select("a.sn");
        for (Element item : parts) {
            String itemUrl = item.attr("href");
            urls.add("http:" + itemUrl);
        }
        return urls;

    }
}
